package com.neuedu.javase.thread;

/***
 * 项目名称:  MyJavaSE 
 * 包：  com.neuedu.javase.thread 
 * date:   2025/11/6   11:28  
 * 描述:  
 *  @author : 张金山
 *  作者博客:   https://blog.fulfill.com.cn
 */
public class StopThread extends Thread {

    boolean running = false;

    public StopThread() {
        this.running = true;
    }

    //设置停止
    public void setStop(){
        this.running = false;
        System.out.println("让线程停止");
    }

    public void run(){
        int index = 0;
        while (running){
            System.out.println(this.getName()+ "   线程正在执行" +(index++));

            //休息
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }



    public static void main(String[] args) {

        StopThread st = new StopThread();
        st.start();


        int index = 0;
        while(true){
            System.out.println("main方法 主线程在执行..."+index);
            index++;
            if(index == 20){
                // st.stop();
                // st.interrupt();
                st.setStop();
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

        }

    }


}
